package cn.edu.scau.cmi.wujiaming.hibernate.sceneBuilderController;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import cn.edu.scau.cmi.wujiaming.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.wujiaming.hibernate.dao.TeacherDAO;
import cn.edu.scau.cmi.wujiaming.hibernate.domain.Student;
import cn.edu.scau.cmi.wujiaming.hibernate.domain.Teacher;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;

public class Controller {

	@FXML
	private Button clickedRetrieveButton;

	@FXML
	private TextField updateNameText;

	@FXML
	private Button commitButton;

	@FXML
	private ChoiceBox selectChoiceBox;

	@FXML
	private ChoiceBox selectTeacherChoiceBox;

	@FXML
	private Button operationButton;

	@FXML
	private Button clickedDeleteButton;

	@FXML
	private Button clickedUpdateButton;

	@FXML
	private TextField inputText;

	@FXML
	private TextArea outputText;

	@FXML
	private Button clickedCreateButton;

	private ObservableList<String> observableList = FXCollections.observableArrayList("Teacher", "Student");

	@FXML
	public void openChoiceBox(MouseEvent event) {
		selectChoiceBox.setItems(observableList);
	}

	@FXML
	public void clickedCreateButton(MouseEvent event) {
		operationButton.setText("Create");
		if (selectChoiceBox.getSelectionModel().getSelectedItem().equals("Student")) {
			inputText.setPromptText("请输入创建的Student的名字");
			List<Teacher> list = TeacherDAO.findAllTeacher();
			Iterator<Teacher> teacherIterator = list.iterator();
			List<String> teacherNameList = new ArrayList<>();
			while (teacherIterator.hasNext()) {
				teacherNameList.add(teacherIterator.next().getName());
			}

			ObservableList<String> observableList = FXCollections.observableList(teacherNameList);
			selectTeacherChoiceBox.setVisible(true);
			selectTeacherChoiceBox.setItems(observableList);
		} else {
			inputText.setPromptText("请输入创建的Teacher的名字");
			selectTeacherChoiceBox.setVisible(false);
		}
	}

	@FXML
	public void clickedDeleteButton(MouseEvent event) {
		operationButton.setText("Delete");
		if (selectChoiceBox.getSelectionModel().getSelectedItem().equals("Student")) {
			inputText.setPromptText("请输入要删除的Student的名字");
		} else {
			inputText.setPromptText("请输入要删除的Teacher的名字");
		}
		selectTeacherChoiceBox.setVisible(false);
	}

	@FXML
	public void clickedRetrieveButton(MouseEvent event) {
		operationButton.setText("Retrieve");
		if (selectChoiceBox.getSelectionModel().getSelectedItem().equals("Student")) {
			inputText.setPromptText("请输入要查询的Student的名字");
		} else {
			inputText.setPromptText("请输入要查询的Teacher的名字");
		}
		selectTeacherChoiceBox.setVisible(false);
	}

	@FXML
	public void clickedUpdateButton(MouseEvent event) {
		operationButton.setText("Update");
		if (selectChoiceBox.getSelectionModel().getSelectedItem().equals("Student")) {
			inputText.setPromptText("请输入要重新编辑的Student的名字");
			List<Teacher> list = TeacherDAO.findAllTeacher();
			Iterator<Teacher> teacherIterator = list.iterator();
			List<String> teacherNameList = new ArrayList<>();
			while (teacherIterator.hasNext()) {
				teacherNameList.add(teacherIterator.next().getName());
			}

			ObservableList<String> observableList = FXCollections.observableList(teacherNameList);
			selectTeacherChoiceBox.setVisible(true);
			selectTeacherChoiceBox.setItems(observableList);
		} else {
			inputText.setPromptText("请输入要重新编辑的Teacher的名字");
			selectTeacherChoiceBox.setVisible(false);
		}
		updateNameText.setVisible(true);
	}

	@FXML
	public void commit(MouseEvent event) {
		if (selectChoiceBox.getSelectionModel().getSelectedItem().equals("Student")) {
			Student student;
			switch (operationButton.getText()) {
			case "Create":
				student = new Student();
				student.setName(inputText.getText());
				student.setTeacher(TeacherDAO
						.findTeacherByName(selectTeacherChoiceBox.getSelectionModel().getSelectedItem().toString()));
				StudentDAO.save(student);
				outputText.setText(student.toString() + "已添加");
				break;
			case "Delete":
				student = StudentDAO.findStudentByName(inputText.getText());
				StudentDAO.delete(student);
				outputText.setText(student.toString() + "已删除");
				break;
			case "Retrieve":
				student = StudentDAO.findStudentByName(inputText.getText());
				outputText.setText(student.toString() + "已找到");
				break;
			case "Update":
				student = StudentDAO.findStudentByName(inputText.getText());
				Teacher teacher = TeacherDAO
						.findTeacherByName(selectTeacherChoiceBox.getSelectionModel().getSelectedItem().toString());
				student.setTeacher(teacher);
				student.setName(updateNameText.getText());
				StudentDAO.update(student);
				updateNameText.setVisible(false);
				outputText.setText(student.toString() + "已修改");
				break;
			default:
				break;
			}
		} else {
			Teacher teacher;
			switch (operationButton.getText()) {
			case "Create":
				teacher = new Teacher();
				teacher.setName(inputText.getText());
				TeacherDAO.save(teacher);
				outputText.setText(teacher.toString() + "已添加");
				break;
			case "Delete":
				teacher = TeacherDAO.findTeacherByName(inputText.getText());
				TeacherDAO.delete(teacher);
				outputText.setText(teacher.toString() + "已删除");
				break;
			case "Retrieve":
				teacher = TeacherDAO.findTeacherByName(inputText.getText());
				outputText.setText(teacher.toString() + "已找到");
				break;
			case "Update":
				teacher = TeacherDAO.findTeacherByName(inputText.getText());
				teacher.setName(updateNameText.getText());
				TeacherDAO.update(teacher);
				outputText.setText(teacher.toString() + "已修改");
				updateNameText.setVisible(false);
				break;
			default:
				break;
			}
		}
	}
}